package swd.work.generator.v1.page;

import java.io.*;
import java.util.*;

import com.pub.database.*;
import com.pub.tools.*;

import swd.work.generator.v1.java.base.*;

public class JspGenerator extends PageExtend implements PageImplement {

    public JspGenerator(QueryBean pqbnDAO, String pstrUserNO) throws Exception {
        super(pqbnDAO, pstrUserNO);
    }

    /**
     * createPageBody
     *
     * @param pbwrPrgNO BufferedWriter
     * @param pstrPrgNO String
     */
    private void createPageBody(BufferedWriter pbwrPrgNO, String pstrPrgNO) throws
            Exception {
        Hashtable<String,Hashtable<String,Object>> lhtWKF_FRM0 = null;
        Hashtable<String,Object> lhtWKF_FRM0_DATA = null;
        int lintFrm0Cnt = 0;

        try {
            lhtWKF_FRM0 = gqbnDAO.hashtableSQL(
                    "select frm_no,record_kind,frm_name from wkf_frm0 where prg_no='" +
                    pstrPrgNO + "' order by seqno");

            while (lhtWKF_FRM0 != null &&
                   lhtWKF_FRM0.get(String.valueOf(lintFrm0Cnt)) != null) {
                lhtWKF_FRM0_DATA = (Hashtable<String,Object>) lhtWKF_FRM0.get(String.valueOf(
                        lintFrm0Cnt));

                if (lhtWKF_FRM0_DATA.get("FRM_NAME") != null &&
                    !lhtWKF_FRM0_DATA.get("FRM_NAME").toString().trim().equals(
                            "")) {
                    outputFrmName(pbwrPrgNO, lintFrm0Cnt + 1);
                }

                super.outputPageTag(pbwrPrgNO, gstbTagTabStr, gstrTabChar,
                                    "<TABLE BORDER=\"0\" WIDTH=\"540px\" CELLSPACING=\"1\" CELLPADDING=\"0\" >",
                                    1);

                if (lhtWKF_FRM0_DATA.get("RECORD_KIND").toString().equals("S")) { //FreeForm
                    createPageBodyFree(pbwrPrgNO, pstrPrgNO,
                                       lhtWKF_FRM0_DATA.get("FRM_NO").toString(),
                                       gstrSysNO);
                } else { //GridForm

                }

                super.outputPageTag(pbwrPrgNO, gstbTagTabStr, gstrTabChar,
                                    "</TABLE>", 1);

                outputHideenField(pbwrPrgNO, pstrPrgNO,
                                  lhtWKF_FRM0_DATA.get("FRM_NO").toString(),
                                  gstrSysNO);

                lintFrm0Cnt++;
                //break;
            }

            outputPagePublicField(pbwrPrgNO); //輸出共用欄位
            //return "";
        } catch (Exception e) {
            throw new Exception(e.toString());
            //return "fail";
        } finally {
            lhtWKF_FRM0 = null;
            lhtWKF_FRM0_DATA = null;
        }
    }

    /**
     * outputHideenField
     *
     * @param pbwrPrgNO BufferedWriter
     * @param pstrPrgNO String
     * @param pstrFrmNO String
     * @param pstrSysNO String
     */
    private void outputHideenField(BufferedWriter pbwrPrgNO, String pstrPrgNO,
                                   String pstrFrmNO, String pstrSysNO) throws
            Exception {
        Hashtable<String,Hashtable<String,Object>> lhtWKF_FRM1 = null;
        Hashtable<String,Object> lhtWKF_FRM1_DATA = null;
        int lintWKF_FRM1_cnt = 0;
        //Hashtable lhtFldRwModeCss = null;
        String lstrFldName = null;

        try {
            lhtWKF_FRM1 = gqbnDAO.hashtableSQL(
                    "select * from wkf_frm1 where prg_no='" + pstrPrgNO +
                    "' and rw_mode='H' and frm_no='" + pstrFrmNO +
                    "' order by fld_row_pos,tab_order");

            while (lhtWKF_FRM1 != null &&
                   lhtWKF_FRM1.get(String.valueOf(lintWKF_FRM1_cnt)) != null) {
                lhtWKF_FRM1_DATA = (Hashtable<String,Object>) lhtWKF_FRM1.get(String.valueOf(
                        lintWKF_FRM1_cnt));

                lintWKF_FRM1_cnt++;
                /*Hidden 欄位不用套css
                 lhtFldRwModeCss = get_edit_mode_css(lhtWKF_FRM1_DATA.get(
                        "RW_MODE").toString(),
                        lhtWKF_FRM1_DATA.get("FLD_KIND").
                        toString(),
                        (lhtWKF_FRM1_DATA.get("OPEN_DB") == null ?
                         "" :
                         lhtWKF_FRM1_DATA.get("OPEN_DB").
                         toString()),
                        (lhtWKF_FRM1_DATA.get("GET_FLD") == null ?
                         "" :
                         lhtWKF_FRM1_DATA.get("GET_FLD").
                         toString()),
                        (lhtWKF_FRM1_DATA.get(
                                "READ_ONLY_YN") == null ?
                         "" :
                         lhtWKF_FRM1_DATA.get(
                                 "READ_ONLY_YN").
                         toString()),
                        (lhtWKF_FRM1_DATA.get("LINK_KIND") == null ?
                         "" :
                         lhtWKF_FRM1_DATA.get("LINK_KIND").
                         toString()));*/

                lstrFldName = lhtWKF_FRM1_DATA.get("FLD_NO").toString();

                super.outputPageTag(pbwrPrgNO, gstbTagTabStr, gstrTabChar,
                                    "<INPUT TYPE=\"HIDDEN\" NAME=\"" +
                                    lstrFldName +
                                    "\" SIZE=\"" +
                                    lhtWKF_FRM1_DATA.get("FLD_LEN").
                                    toString() + "\" MAXLENGTH=\"" +
                                    lhtWKF_FRM1_DATA.get("FLD_MAX_LEN").
                                    toString() + "\" VALUE=\"<%=(view.get" +
                                    lstrFldName + "()!=null ? view.get" +
                                    lstrFldName +
                                    "() : \"\")%>\" />", 1);
                        /* Hidden 欄位不用判斷是否隱藏
                                                    <%=view.get" +
                                                    lstrFldName +
                "_readonly() ? \"readonly\" : \"\"%> CLASS=\"<%=(view.get" +
                lstrFldName + "_readonly() ? \"" +
                lhtFldRwModeCss.get("CSS").toString() +
                                                    "_noborder\" : \"" +
                lhtFldRwModeCss.get("CSS").toString() +
                                                    "\")%>\" />", 1);*/
            }
        } catch (Exception e) {
            throw new Exception(e);
        } finally {
            if (lhtWKF_FRM1 != null) {
                lhtWKF_FRM1.clear();
                lhtWKF_FRM1 = null;
            }
        }
    }

    /**
     * outputFrmName
     *
     * @param pintFrm0Cnt int
     */
    private void outputFrmName(BufferedWriter pbwrPrgNO, int pintFrm0Cnt) {
        super.outputPageTag(pbwrPrgNO, gstbTagTabStr, gstrTabChar,
                            "<TABLE BORDER=\"0\" WIDTH=\"540px\" CELLSPACING=\"1\" CELLPADDING=\"0\" >",
                            1);
        super.outputPageTag(pbwrPrgNO, gstbTagTabStr, gstrTabChar,
                            "<TR >",
                            1);
        super.outputPageTag(pbwrPrgNO, gstbTagTabStr, gstrTabChar,
                            "<TD WIDTH=\"69\"><DIV ALIGN=\"LEFT\"><IMG SRC=\"/PUBPAGE/pub_images/title/title02_bg1.gif\" WIDTH=\"69\" HEIGHT=\"11\" alt=\"\"></DIV></TD>",
                            1);
        super.outputPageTag(pbwrPrgNO, gstbTagTabStr, gstrTabChar,
                            "<TD VALIGN=\"MIDDLE\" CLASS=\"TITLE\"><%=view.getFrm" +
                            String.valueOf(pintFrm0Cnt) +
                            "Name()%></TD>",
                            1);
        super.outputPageTag(pbwrPrgNO, gstbTagTabStr, gstrTabChar,
                            "<TD WIDTH=\"69\"><DIV ALIGN=\"RIGHT\"><IMG SRC=\"/PUBPAGE/pub_images/title/title2_bg2.gif\" WIDTH=\"69\" HEIGHT=\"11\" alt=\"\"></DIV></TD>",
                            1);
        super.outputPageTag(pbwrPrgNO, gstbTagTabStr, gstrTabChar,
                            "</TR></TABLE><BR/>",
                            1);
    }

    /**
     * createPageBodyFree
     *
     * @param pbwrPrgNO BufferedWriter
     * @param phtWKF_FRM1 Hashtable
     * @param pstrSysNO String
     */
    private void createPageBodyFree(BufferedWriter pbwrPrgNO, String pstrPrgNO,
                                    String pstrFrmNO, String pstrSysNO) throws
            Exception {
        Hashtable<String,Hashtable<String,Object>> lhtWKF_FRM1 = null;
        Hashtable<String,Object> lhtWKF_FRM1_DATA = null;
        int lintWKF_FRM1_cnt = 0;
        Hashtable<String,String> lhtFldRwModeCss = null;
        String lstrFldName = null;

        try {
            lhtWKF_FRM1 = gqbnDAO.hashtableSQL(
                    "select * from wkf_frm1 where prg_no='" + pstrPrgNO +
                    "' and rw_mode<>'H' and frm_no='" + pstrFrmNO +
                    "' order by fld_row_pos,tab_order");

            while (lhtWKF_FRM1 != null &&
                   lhtWKF_FRM1.get(String.valueOf(lintWKF_FRM1_cnt)) != null) {
                lhtWKF_FRM1_DATA = (Hashtable<String,Object>) lhtWKF_FRM1.get(String.valueOf(
                        lintWKF_FRM1_cnt));

                lhtFldRwModeCss = get_edit_mode_css(lhtWKF_FRM1_DATA.get(
                        "RW_MODE").toString(),
                        lhtWKF_FRM1_DATA.get("FLD_KIND").toString(),
                        (lhtWKF_FRM1_DATA.get("OPEN_DB") == null ?
                         "" :
                         lhtWKF_FRM1_DATA.get("OPEN_DB").toString()),
                        (lhtWKF_FRM1_DATA.get("GET_FLD") == null ?
                         "" :
                         lhtWKF_FRM1_DATA.get("GET_FLD").toString()),
                        (lhtWKF_FRM1_DATA.get("READ_ONLY_YN") == null ?
                         "" :
                         lhtWKF_FRM1_DATA.get("READ_ONLY_YN").
                         toString()),
                        (lhtWKF_FRM1_DATA.get("LINK_KIND") == null ?
                         "" :
                         lhtWKF_FRM1_DATA.get("LINK_KIND").toString()));

                lstrFldName = lhtWKF_FRM1_DATA.get("FLD_NO").toString();

                //輸出此欄位前需換行lhtWKF_FRM1_DATA.get("JMP_LINE_YN").toString().equals("Y")
                if (lintWKF_FRM1_cnt % 2 == 0 ||
                    lintWKF_FRM1_cnt == 0) {
                    if (lintWKF_FRM1_cnt != 0) {
                        super.outputPageTag(pbwrPrgNO, gstbTagTabStr,
                                            gstrTabChar,
                                            "</TR>", 1);
                    }
                    super.outputPageTag(pbwrPrgNO, gstbTagTabStr, gstrTabChar,
                                        "<TR>", 1);
                }

                //輸出TD Html
                //NoteLine另外輸出自己的TD Html code
                if (!lhtFldRwModeCss.get("EDITMODE").toString().equals(
                        "NOTELINE") &&
                    !lhtFldRwModeCss.get("EDITMODE").toString().equals("RESET") &&
                    !lhtFldRwModeCss.get("EDITMODE").toString().equals("SUBMIT") &&
                    !lhtFldRwModeCss.get("EDITMODE").toString().equals("BUTTON")
                        ) {
                    super.outputPageTag(pbwrPrgNO, gstbTagTabStr, gstrTabChar,
                                        "<TD nowrap WIDTH=\"" +
                                        lhtWKF_FRM1_DATA.get("T_PERCENT") +
                                        "%\" CLASS=\"td_head\">", 1);
                    super.outputPageTag(pbwrPrgNO, gstbTagTabStr, gstrTabChar,
                                        "<%=view.get" + lstrFldName +
                                        "Title()%>", 0);
                    super.outputPageTag(pbwrPrgNO, gstbTagTabStr, gstrTabChar,
                                        "</TD>", 0);
                    super.outputPageTag(pbwrPrgNO, gstbTagTabStr, gstrTabChar,
                                        "<TD WIDTH=\"" +
                                        lhtWKF_FRM1_DATA.get("F_PERCENT") +
                                        "%\" CLASS=\"td_content\" " +
                                        (
                                                lhtFldRwModeCss.get("EDITMODE").
                                                equals("RICHTEXT") ||
                                                lhtFldRwModeCss.get("EDITMODE").
                                                equals(
                            "TEXTAREA") ?
                                                "colspan=\"" +
                                                String.valueOf(
                            getFormMaxColSize(pstrPrgNO, pstrFrmNO) *
                            2 - 1) + "\" " : "") + ">", 1);
                }

                if (lhtFldRwModeCss.get("EDITMODE").toString().equals("FILE")) {
                    super.outputPageTag(pbwrPrgNO, gstbTagTabStr, gstrTabChar,
                                        "<INPUT TYPE=\"BUTTON\" NAME=\"" +
                                        lstrFldName +
                                        "\" VALUE=\"<%=view.getBtnUploadTitle()%>\" CLASS=\"" +
                                        lhtFldRwModeCss.get("CSS").toString() +
                                        "\" onclick='javascript:document.all.item(\"prgAction\").value=\"upload_" +
                                        lstrFldName +
                                        "\";document.forms[0].submit();'/>", 1);
                }

                if (lhtFldRwModeCss.get("EDITMODE").toString().equals("TEXT")) {
                    super.outputPageTag(pbwrPrgNO, gstbTagTabStr, gstrTabChar,
                                        "<INPUT TYPE=\"" +
                                        lhtFldRwModeCss.get("EDITMODE").
                                        toString() + "\" NAME=\"" + lstrFldName +
                                        "\" SIZE=\"" +
                                        lhtWKF_FRM1_DATA.get("FLD_LEN").
                                        toString() + "\" MAXLENGTH=\"" +
                                        lhtWKF_FRM1_DATA.get("FLD_MAX_LEN").
                                        toString() + "\" VALUE=\"<%=(view.get" +
                                        lstrFldName + "()!=" +
                                        (lhtWKF_FRM1_DATA.get("FLD_KIND").
                                         toString().equals("N") ? "0" : "null") +
                                        " ? view.get" +
                                        lstrFldName +
                                        "() : " +
                                        (lhtWKF_FRM1_DATA.get("FLD_KIND").
                                         toString().equals("N") ? "0" : "\"\"") +
                                        ")%>\" " +
                                        (lhtWKF_FRM1_DATA.get("RW_MODE").
                                         toString().equals("V") ?
                                         "readonly class=\"input_noborder\" " :
                                         "<%=view.get" + lstrFldName +
                            "_readonly() ? \"readonly\" : \"\"%> CLASS=\"<%=(view.get" +
                                         lstrFldName + "_readonly() ? \"" +
                                         lhtFldRwModeCss.get("CSS").toString() +
                                         "_noborder\" : \"" +
                                         lhtFldRwModeCss.get("CSS").toString() +
                                         "\")%>\"") + " />", 1);
                }

                if (lhtFldRwModeCss.get("EDITMODE").toString().equals(
                        "CHECKBOX") ||
                    lhtFldRwModeCss.get("EDITMODE").toString().equals("RADIO")) {
                    if (lhtFldRwModeCss.get("EDITMODE").toString().equals(
                            "CHECKBOX") && lhtWKF_FRM1_DATA.get("DD_DATA") == null) {
                        super.outputPageTag(pbwrPrgNO, gstbTagTabStr,
                                            gstrTabChar,
                                            "<INPUT TYPE=\"CHECKBOX\" NAME=\"" +
                                            lstrFldName +
                                            "\" VALUE=\"\" CLASS=\"" +
                                            lhtFldRwModeCss.get("CSS").toString() +
                                            "\"/>", 1);
                    } else {
                        output_chk_opt_data(pbwrPrgNO,
                                            lhtWKF_FRM1_DATA.get("DD_DATA").
                                            toString(), lstrFldName,
                                            lhtFldRwModeCss.get("EDITMODE").
                                            toString(),
                                            lhtFldRwModeCss.get("CSS").toString(),
                                            pstrSysNO);
                    }
                }

                if (lhtFldRwModeCss.get("EDITMODE").toString().equals("LINK")) {

                }

                if (lhtFldRwModeCss.get("EDITMODE").toString().equals("RESET") ||
                    lhtFldRwModeCss.get("EDITMODE").toString().equals("SUBMIT") ||
                    lhtFldRwModeCss.get("EDITMODE").toString().equals("BUTTON")) {
                    super.outputPageTag(pbwrPrgNO, gstbTagTabStr, gstrTabChar,
                                        "<INPUT TYPE=\"" +
                                        lhtFldRwModeCss.get("EDITMODE").
                                        toString() + "\" NAME=\"" + lstrFldName +
                                        "\" VALUE=\"<%=view.get" + lstrFldName +
                                        "Title()%>\" CLASS=\"" +
                                        lhtFldRwModeCss.get("CSS").toString() +
                                        "\"/>", 1);
                }

                if (lhtFldRwModeCss.get("EDITMODE").toString().equals(
                        "NOTELINE")) {
                    //
                }

                if (lhtFldRwModeCss.get("EDITMODE").toString().equals("SELECT")) {
                    super.outputPageTag(pbwrPrgNO, gstbTagTabStr, gstrTabChar,
                                        "<SELECT NAME=\"" + lstrFldName +
                                        "\" SIZE=\"" +
                                        PublicFunction.
                                        getDesSplitString(
                                                lhtWKF_FRM1_DATA.get("FLD_LEN").
                                                toString(), ",", 0) +
                                        "\" onfocus=\"javascript:if(<%=view.get" +
                                        lstrFldName +
                                        "_readonly()%>==1){this.blur();}\" CLASS=\"" +
                                        lhtFldRwModeCss.get("CSS").toString() +
                                        "\">", 1);
                    if (lhtWKF_FRM1_DATA.get("DD_DATA") != null &&
                        !lhtWKF_FRM1_DATA.get("DD_DATA").toString().equals("")) {
                        if (lhtWKF_FRM1_DATA.get("DD_DATA").toString().
                            substring(0, 5).toUpperCase().equals("SELECT")) {
                            super.outputPageTag(pbwrPrgNO, gstbTagTabStr,
                                                gstrTabChar,
                                                "<%=view.get" + lstrFldName +
                                                "List()%>", 1);
                        } else {
                            output_dropdown_data(pbwrPrgNO,
                                                 lhtWKF_FRM1_DATA.get("DD_DATA").
                                                 toString(),
                                                 "view.get" + lstrFldName +
                                                 "()");
                        }
                    }
                    super.outputPageTag(pbwrPrgNO, gstbTagTabStr, gstrTabChar,
                                        "</SELECT>", 1);
                }

                if (lhtFldRwModeCss.get("EDITMODE").toString().equals(
                        "TEXTAREA")) {
                    super.outputPageTag(pbwrPrgNO, gstbTagTabStr, gstrTabChar,
                                        "<TEXTAREA rows=\"" +
                                        PublicFunction.
                                        getDesSplitString(
                                                lhtWKF_FRM1_DATA.get("FLD_LEN").
                                                toString(), ",", 1) +
                                        "\" cols=\"" +
                                        PublicFunction.getDesSplitString(
                                                lhtWKF_FRM1_DATA.get("FLD_LEN").
                                                toString(), ",", 0) +
                                        "\" NAME=\"" + lstrFldName +
                                        "\" <%=view.get" + lstrFldName +
                                        "_readonly() ? \"readonly\" : \"\"%> CLASS=\"" +
                                        lhtFldRwModeCss.get("CSS").toString() +
                                        "\"><%=(view.get" + lstrFldName +
                                        "()!=null ? view.get" + lstrFldName +
                                        "() : \"\")%></TEXTAREA>", 1);
                }

                if (lhtFldRwModeCss.get("EDITMODE").toString().equals(
                        "RICHTEXT")) {

                }

                super.outputPageTag(pbwrPrgNO, gstbTagTabStr,
                                    gstrTabChar, "</TD>", 1);

                /*if (!lhtFldRwModeCss.get("EDITMODE").toString().equals("HIDDEN")) {
                    super.outputPageTag(pbwrPrgNO, gstbTagTabStr,
                                        gstrTabChar, "</TD>", 1);
                                 }*/

                lintWKF_FRM1_cnt++;
            }

            super.outputPageTag(pbwrPrgNO, gstbTagTabStr,
                                gstrTabChar,
                                "</TR>", 1);

        } catch (Exception e) {
            e.printStackTrace();
            throw new Exception(e);
        }

    }

    /**
     * createPageButton
     *
     * @param pbwrPrgNO BufferedWriter
     * @param pstrPrgNO String
     */
    private void createPageButton(BufferedWriter pbwrPrgNO) throws Exception {
        String lstrBtnCss = get_edit_mode_css("S", "", "", "", "",
                                              "").get("CSS").toString();

        super.outputPageTag(pbwrPrgNO, gstbTagTabStr, gstrTabChar,
                            "<TABLE BORDER=\"0\" CELLSPACING=\"1\" CELLPADDING=\"0\" >",
                            1);
        super.outputPageTag(pbwrPrgNO, gstbTagTabStr, gstrTabChar, "<TR >", 1);
        super.outputPageTag(pbwrPrgNO, gstbTagTabStr, gstrTabChar,
                            "<TD style=\"DISPLAY='<%=view.getBtnAddShow()%>'\"><INPUT TYPE=\"BUTTON\" VALUE=\"<%=view.getBtnAddTitle()%>\" CLASS=\"" +
                            lstrBtnCss + "\" ONCLICK=\"javascript:document.all.item('prgAction').value='add';document.forms[0].submit();\"/></TD>",
                            1);
        super.outputPageTag(pbwrPrgNO, gstbTagTabStr, gstrTabChar,
                            "<TD style=\"DISPLAY='<%=view.getBtnSaveShow()%>'\"><INPUT TYPE=\"BUTTON\" VALUE=\"<%=view.getBtnSaveTitle()%>\" CLASS=\"" +
                            lstrBtnCss + "\" ONCLICK=\"javascript:document.all.item('prgAction').value='save';document.forms[0].submit();\"/></TD>",
                            1);
        super.outputPageTag(pbwrPrgNO, gstbTagTabStr, gstrTabChar,
                            "<TD style=\"DISPLAY='<%=view.getBtnDraftShow()%>'\"><INPUT TYPE=\"BUTTON\" VALUE=\"<%=view.getBtnDraftTitle()%>\" CLASS=\"" +
                            lstrBtnCss + "\" ONCLICK=\"javascript:document.all.item('prgAction').value='draft';document.forms[0].submit();\"/></TD>",
                            1);
        super.outputPageTag(pbwrPrgNO, gstbTagTabStr, gstrTabChar,
                            "<TD style=\"DISPLAY='<%=view.getBtnQueryShow()%>'\"><INPUT TYPE=\"BUTTON\" VALUE=\"<%=view.getBtnQueryTitle()%>\" CLASS=\"" +
                            lstrBtnCss + "\" ONCLICK=\"javascript:document.all.item('prgAction').value='query';document.forms[0].submit();\"/></TD>",
                            1);
        super.outputPageTag(pbwrPrgNO, gstbTagTabStr, gstrTabChar,
                            "<TD style=\"DISPLAY='<%=view.getBtnModifyShow()%>'\"><INPUT TYPE=\"BUTTON\" VALUE=\"<%=view.getBtnModifyTitle()%>\" CLASS=\"" +
                            lstrBtnCss + "\" ONCLICK=\"javascript:document.all.item('prgAction').value='modify';document.forms[0].submit();\"/></TD>",
                            1);
        super.outputPageTag(pbwrPrgNO, gstbTagTabStr, gstrTabChar,
                            "<TD style=\"DISPLAY='<%=view.getBtnApproveShow()%>'\"><INPUT TYPE=\"BUTTON\" VALUE=\"<%=view.getBtnApproveTitle()%>\" CLASS=\"" +
                            lstrBtnCss + "\" ONCLICK=\"javascript:document.all.item('prgAction').value='approve';document.forms[0].submit();\"/></TD>",
                            1);
        super.outputPageTag(pbwrPrgNO, gstbTagTabStr, gstrTabChar,
                            "<TD style=\"DISPLAY='<%=view.getBtnRejectShow()%>'\"><INPUT TYPE=\"BUTTON\" VALUE=\"<%=view.getBtnRejectTitle()%>\" CLASS=\"" +
                            lstrBtnCss + "\" ONCLICK=\"javascript:document.all.item('prgAction').value='reject';document.forms[0].submit();\"/></TD>",
                            1);
        super.outputPageTag(pbwrPrgNO, gstbTagTabStr, gstrTabChar,
                            "<TD style=\"DISPLAY='<%=view.getBtnCancelShow()%>'\"><INPUT TYPE=\"BUTTON\" VALUE=\"<%=view.getBtnCancelTitle()%>\" CLASS=\"" +
                            lstrBtnCss + "\" ONCLICK=\"javascript:document.all.item('prgAction').value='cancel';document.forms[0].submit();\"/></TD>",
                            1);
        super.outputPageTag(pbwrPrgNO, gstbTagTabStr, gstrTabChar,
                            "<TD style=\"DISPLAY='<%=view.getBtnNoteShow()%>'\"><INPUT TYPE=\"BUTTON\" VALUE=\"<%=view.getBtnNoteTitle()%>\" CLASS=\"" +
                            lstrBtnCss + "\" ONCLICK=\"javascript:window.showModalDialog('/PUBPAGE/flow_note_add.jsp?note_kind=L&rights=1&flow_num=<%=view.getflow_num()%>&flow_no=N&rout_no=1&r_empno=<%=request.getSession().getAttribute(\"u_id\").toString()%>','notePage','status:No;dialogWidth:640px;dialogHeight:500px;center:yes;scrollbars=yes');\"/></TD>",
                            1);
        super.outputPageTag(pbwrPrgNO, gstbTagTabStr, gstrTabChar,
                            "<TD style=\"DISPLAY='<%=view.getBtnTraceShow()%>'\"><INPUT TYPE=\"BUTTON\" VALUE=\"<%=view.getBtnTraceTitle()%>\" CLASS=\"" +
                            lstrBtnCss + "\" ONCLICK=\"javascript:window.open('/PUBPAGE/wkf_urge.jsp?flownum=<%=view.getflow_num()%>&db_alias=<%=view.getDBAlias() %>&debug=<%=view.getDBDebug()%>&source_charset=<%=view.getSourceCharset()%>&target_charset=<%=view.getTargetCharset() %>','_blank','toolbar=no,location=no,directories=no,scrollbars=yes,width=880,height=600');\"/></TD>",
                            1);
        super.outputPageTag(pbwrPrgNO, gstbTagTabStr, gstrTabChar,
                            "<TD style=\"DISPLAY='<%=view.getBtnExitShow()%>'\"><INPUT TYPE=\"BUTTON\" VALUE=\"<%=view.getBtnExitTitle()%>\" CLASS=\"" +
                            lstrBtnCss + "\" ONCLICK=\"javascript:document.all.item('prgAction').value='exit';document.forms[0].submit();\"/></TD>",
                            1);
        super.outputPageTag(pbwrPrgNO, gstbTagTabStr, gstrTabChar, "</TR >", 1);
        super.outputPageTag(pbwrPrgNO, gstbTagTabStr, gstrTabChar, "</TABLE >",
                            1);
    }

    /**
     * createPageEnd
     *
     * @param pbwrPrgNO BufferedWriter
     */
    private void createPageEnd(BufferedWriter pbwrPrgNO) throws Exception {
        super.outputPageTag(pbwrPrgNO, gstbTagTabStr, gstrTabChar, "</form>", 1);
        super.outputPageTag(pbwrPrgNO, gstbTagTabStr, gstrTabChar, "</body>", 1);
        super.outputPageTag(pbwrPrgNO, gstbTagTabStr, gstrTabChar, "</html>", 1);
    }

    /**
     * createPageHeader
     *
     * @param pbwrPrgNO BufferedWriter
     */
    private void createPageHeader(BufferedWriter pbwrPrgNO, String pstrPrgNO) throws
            Exception {
        try {
            super.outputPageTag(pbwrPrgNO, gstbTagTabStr, gstrTabChar,
                                "<%@ page contentType=\"text/html; charset=UTF-8\"%>",
                                0);
            super.outputPageTag(pbwrPrgNO, gstbTagTabStr, gstrTabChar,
                                "<jsp:useBean id=\"action\" scope=\"request\" class=\"com.swd." +
                                gstrSysNO + "." + pstrPrgNO + ".Action\" />", 1);
            super.outputPageTag(pbwrPrgNO, gstbTagTabStr, gstrTabChar,
                                "<jsp:useBean id=\"view\" scope=\"request\" class=\"com.swd." +
                                gstrSysNO + "." + pstrPrgNO + ".View\" />", 1);
            super.outputPageTag(pbwrPrgNO, gstbTagTabStr, gstrTabChar,
                                "<jsp:setProperty name=\"view\" property=\"*\" />",
                                1);
            super.outputPageTag(pbwrPrgNO, gstbTagTabStr, gstrTabChar,
                                "<%if (action.action(pageContext).equals(\"login\")) {return;} %>",
                                1);
            /*super.outputPageTag(pbwrPrgNO, gstbTagTabStr, gstrTabChar, "<jsp:useBean id=\"qbnDAO\" scope=\"request\" type=\"com.pub.database.QueryBean\" />",
                                1);*/
            super.outputPageTag(pbwrPrgNO, gstbTagTabStr, gstrTabChar, "<html>",
                                2);
            super.outputPageTag(pbwrPrgNO, gstbTagTabStr, gstrTabChar, "<head>",
                                1);
            super.outputPageTag(pbwrPrgNO, gstbTagTabStr, gstrTabChar,
                                "<title><%=view.getPrgTitle()%></title>", 1);
            super.outputPageTag(pbwrPrgNO, gstbTagTabStr, gstrTabChar,
                                "<LINK HREF=\"/PUBPAGE/general/workflow.css\" REL=\"STYLESHEET\" TYPE=\"TEXT/CSS\">",
                                1);
            super.outputPageTag(pbwrPrgNO, gstbTagTabStr, gstrTabChar,
                                "<!--小日曆用script，若此程式有使用挑選日期欄位，請解除remark",
                                1);
            super.outputPageTag(pbwrPrgNO, gstbTagTabStr, gstrTabChar,
                                "<script type=\"text/javascript\" language=\"javascript1.2\" src=\"/PUBPAGE/general/calendar.js\"></script-->",
                                1);
            super.outputPageTag(pbwrPrgNO, gstbTagTabStr, gstrTabChar,
                                "<!--共用script，若此程式有使用一般性檢查及格式轉換，請解除remark",
                                1);
            super.outputPageTag(pbwrPrgNO, gstbTagTabStr, gstrTabChar,
                                "<script type=\"text/javascript\" language=\"javascript1.2\" src=\"/PUBPAGE/general/commscript.js\" ></script-->",
                                1);
            super.outputPageTag(pbwrPrgNO, gstbTagTabStr, gstrTabChar,
                                "<!--html 操作 script，通常使用於AJAX動態即時變更html內容(如下拉欄位的值)，若有使用，請解除remark",
                                1);
            super.outputPageTag(pbwrPrgNO, gstbTagTabStr, gstrTabChar,
                                "<script type=\"text/javascript\" language=\"javascript1.2\" src=\"/PUBPAGE/general/commhtmlscript.js\" ></script-->",
                                1);
            super.outputPageTag(pbwrPrgNO, gstbTagTabStr, gstrTabChar,
                                "<!--JSP default submit self -->",
                                1);
            super.outputPageTag(pbwrPrgNO, gstbTagTabStr, gstrTabChar,
                                "<base target='_self' />",
                                1);
            super.outputPageTag(pbwrPrgNO, gstbTagTabStr, gstrTabChar,
                                "</head>", 1);
            super.outputPageTag(pbwrPrgNO, gstbTagTabStr, gstrTabChar,
                                "<body bgcolor=\"#ffffff\" onload=\"<%=view.getOnloadScript()%>\">",
                                1);
            super.outputPageTag(pbwrPrgNO, gstbTagTabStr, gstrTabChar,
                                "<TABLE BORDER=\"0\" HEIGHT=\"20\" CELLPADDING=\"0\" CELLSPACING=\"0\">",
                                1);
            super.outputPageTag(pbwrPrgNO, gstbTagTabStr, gstrTabChar,
                                "<TR><TD  BACKGROUND=\"/PUBPAGE/pub_images/title/title_bg.gif\"><DIV ALIGN=\"LEFT\"><IMG SRC=\"/PUBPAGE/pub_images/title/title01.gif\" WIDTH=\"18\" HEIGHT=\"24\" HSPACE=\"0\" alt=\"\"/><IMG SRC=\"/PUBPAGE/pub_images/glass.gif\" WIDTH=\"12\" HEIGHT=\"8\" alt=\"\"/></DIV></TD>",
                                1);
            super.outputPageTag(pbwrPrgNO, gstbTagTabStr, gstrTabChar,
                                "<TD ALIGN=\"LEFT\" HEIGHT=\"20\" VALIGN=\"MIDDLE\" BACKGROUND=\"/PUBPAGE/pub_images/title/title_bg.gif\" CLASS=\"TITLE\">",
                                1);
            super.outputPageTag(pbwrPrgNO, gstbTagTabStr, gstrTabChar,
                                "<%=view.getPrgTitle()%>",
                                0);
            super.outputPageTag(pbwrPrgNO, gstbTagTabStr, gstrTabChar,
                                "</TD>",
                                0);
            super.outputPageTag(pbwrPrgNO, gstbTagTabStr, gstrTabChar,
                                "<TD BACKGROUND=\"/PUBPAGE/pub_images/title/title_bg.gif\"><DIV ALIGN=\"RIGHT\"><IMG SRC=\"/PUBPAGE/pub_images/title/glass.gif\" WIDTH=\"12\" HEIGHT=\"8\" alt=\"\"/><IMG SRC=\"/PUBPAGE/pub_images/title/title02.gif\" WIDTH=\"18\" HEIGHT=\"24\" HSPACE=\"0\" alt=\"\"/></DIV></TD>",
                                1);
            super.outputPageTag(pbwrPrgNO, gstbTagTabStr, gstrTabChar,
                                "</TR></TABLE><br/>",
                                1);
            super.outputPageTag(pbwrPrgNO, gstbTagTabStr, gstrTabChar,
                                "<form action=\"<%=request.getRequestURI() %>\" method=\"POST\">", 1);
        } catch (Exception e) {
            throw new Exception(e.toString());
        }
    }

    /**
     * outputPagePublicField
     *
     * @param pbwrPrgNO BufferedWriter
     */
    private void outputPagePublicField(BufferedWriter pbwrPrgNO) {
        try {
            super.outputPageTag(pbwrPrgNO, gstbTagTabStr, gstrTabChar,
                                "<INPUT TYPE=\"HIDDEN\" NAME=\"prgAction\" VALUE=\"<%=view.getPrgAction()%>\" />",
                                1);
            super.outputPageTag(pbwrPrgNO, gstbTagTabStr, gstrTabChar,
                                "<INPUT TYPE=\"HIDDEN\" NAME=\"prgStatus\" VALUE=\"<%=view.getPrgStatus()%>\" />",
                                1);
            super.outputPageTag(pbwrPrgNO, gstbTagTabStr, gstrTabChar,
                                "<INPUT TYPE=\"HIDDEN\" NAME=\"flowAction\" VALUE=\"<%=view.getFlowAction()%>\" />",
                                1);
            super.outputPageTag(pbwrPrgNO, gstbTagTabStr, gstrTabChar,
                                "<INPUT TYPE=\"HIDDEN\" NAME=\"flowStatus\" VALUE=\"<%=view.getFlowStatus()%>\" />",
                                1);
            super.outputPageTag(pbwrPrgNO, gstbTagTabStr, gstrTabChar,
                                "<INPUT TYPE=\"HIDDEN\" NAME=\"flowNO\" VALUE=\"<%=view.getFlowNO()%>\" />",
                                1);
            super.outputPageTag(pbwrPrgNO, gstbTagTabStr, gstrTabChar,
                                "<INPUT TYPE=\"HIDDEN\" NAME=\"portal\" VALUE=\"<%=view.getportal()%>\" />",
                                1);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private void output_chk_opt_data(BufferedWriter pbwrPrgNO,
                                     String pstrDDData, String pstrFldName,
                                     String pstrFldType, String pstrCss,
                                     String pstrSysNO) throws Exception {
        String[] lstrSplitData = null;
        String lstrRadioDtl = null;
        String lstrRadioVal = null;

        try {
            if (pstrDDData == null || pstrDDData.trim().equals("")) {
                throw new Exception("no option data");
            }

            if (pstrDDData.indexOf("/") >= 0 &&
                pstrDDData.split("/")[0].indexOf(",") < 0) { //單純只做true/false判斷
                outputPageTag(pbwrPrgNO, gstbTagTabStr, gstrTabChar,
                              "<INPUT TYPE=\"" + pstrFldType + "\" NAME=\"" +
                              pstrFldName + "\" VALUE=\"" +
                              pstrDDData.split("/")[0] +
                              "\" CLASS=\"" + pstrCss + "\" <%if(view.get" +
                              pstrFldName + "()!=null){out.print((view.get" +
                              pstrFldName + "().equals(\"" +
                              pstrDDData.split("/")[0] +
                              "\") ? \"checked\" : \"\"));}%>/>",
                              1);
                return;
            }

            lstrSplitData = pstrDDData.split("/");

            for (int i = 0; i <= lstrSplitData.length - 1; i++) {
                lstrRadioDtl = PublicFunction.getDesSplitString(lstrSplitData[i],
                        ",", 0);
                lstrRadioVal = PublicFunction.getDesSplitString(lstrSplitData[i],
                        ",", 1);

                outputPageTag(pbwrPrgNO, gstbTagTabStr, gstrTabChar,
                              "<INPUT TYPE=\"" + pstrFldType + "\" NAME=\"" +
                              pstrFldName + "\" VALUE=\"" + lstrRadioVal +
                              "\" CLASS=\"" + pstrCss + "\" <%if(view.get" +
                              pstrFldName + "()!=null){out.print((view.get" +
                              pstrFldName + "().equals(\"" + lstrRadioVal +
                              "\") ? \"checked\" : \"\"));}%>/>" + lstrRadioDtl,
                              1);
            }
        } catch (Exception e) {
            throw new Exception(e.toString());
        }
    }

    private void output_dropdown_data(BufferedWriter pbwrPrgNO, String pstrData,
                                      String pstrAtrName) throws Exception {
        String[] lstrOption = null;
        StringBuffer lstbOptions = new StringBuffer();

        try {
            if (pstrData == null || pstrData.trim().equals("")) {
                System.out.println("no drop down data");
            } else {
                lstrOption = pstrData.split("/");

                for (int i = 0; i < lstrOption.length; i++) {
                    lstbOptions.append("<OPTION VALUE=\"");
                    lstbOptions.append(PublicFunction.getDesSplitString(
                            lstrOption[i], ",", 1));
                    lstbOptions.append("\"");
                    lstbOptions.append(" <%if(" + pstrAtrName +
                                       " !=null){out.print((" + pstrAtrName +
                                       ".equals(\"" +
                                       PublicFunction.
                                       getDesSplitString(lstrOption[i], ",", 1) +
                                       "\") ? \"selected\" : \"\"));}%>");
                    lstbOptions.append(">");
                    lstbOptions.append(PublicFunction.getDesSplitString(
                            lstrOption[i], ",", 0));
                    lstbOptions.append("</OPTION>");

                    outputPageTag(pbwrPrgNO, gstbTagTabStr, gstrTabChar,
                                  lstbOptions.toString(), 1);
                    lstbOptions.delete(0, lstbOptions.length());
                }
            }
        } catch (Exception e) {
            throw new Exception(e.toString());
        } finally {
            lstrOption = null;
        }
    }

    public String createPage(String pstrPrgNO) {
        //String lstrCreateResult = "";
        String lstrBeanFileName = pstrPrgNO + ".jsp";
        BufferedWriter lbwrPrgNO = null;
        String lstrBtnPOS = null;

        try {
            lbwrPrgNO = super.getFwrPrgNO(lstrBeanFileName, "jsp_prg_path", "",
                                          gstrUserNO);

            super.setBaseVarible(pstrPrgNO);

            lstrBtnPOS = gqbnDAO.readSingleValueData(
                    "select btn_pos from wkf_prg where prg_no='" + pstrPrgNO +
                    "'");

            createPageHeader(lbwrPrgNO, pstrPrgNO);

            if (lstrBtnPOS.equals("T")) {
                createPageButton(lbwrPrgNO);
            }

            //輸出審核及駁回意見的iframe
            createFlowCommentIframe(lbwrPrgNO);

            createPageBody(lbwrPrgNO, pstrPrgNO);

            if (lstrBtnPOS.equals("B")) {
                createPageButton(lbwrPrgNO);
            }

            //outputPagePublicField(lbwrPrgNO);

            createPageEnd(lbwrPrgNO);

        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                lbwrPrgNO.flush();
                lbwrPrgNO.close();
                lbwrPrgNO = null;
            } catch (Exception e) {
                System.out.println("cant flush JspGenerator.java File!!");
                e.printStackTrace();
            }
        }
        return null;
    }

    /**
     * createFlowCommentIframe
     *
     * @param pbwrPrgNO BufferedWriter
     */
    private void createFlowCommentIframe(BufferedWriter pbwrPrgNO) throws
            Exception {
        super.outputPageTag(pbwrPrgNO, gstbTagTabStr, gstrTabChar,
                            "<iframe id=\"ifr_rej_note_list\" WIDTH=\"540px\" frameborder=\"0\" scrolling=\"no\" marginheight=\"0\" marginwidth=\"0\" onload=\"javascript:if(document.frames('ifr_rej_note_list').document.getElementById('tbl_note_list')==null){document.getElementById('ifr_rej_note_list').height=0;}else{document.getElementById('ifr_rej_note_list').height=document.frames('ifr_rej_note_list').document.getElementById('tbl_note_list').clientHeight;}\" src=\"/PUBPAGE/flow_note_list.jsp?note_kind=N&db_alias=<%=view.getDBAlias()%>&db_debug=<%=view.getDBDebug()%>&source_charset=<%=view.getSourceCharset()%>&target_charset=<%=view.getTargetCharset()%>&flow_num=<%=view.getflow_num()%>\">",
                            1);
        super.outputPageTag(pbwrPrgNO, gstbTagTabStr, gstrTabChar,
                            "</iframe><br />", 1);
        super.outputPageTag(pbwrPrgNO, gstbTagTabStr, gstrTabChar,
                            "<iframe id=\"ifr_app_note_list\" WIDTH=\"540px\" frameborder=\"0\" scrolling=\"no\" marginheight=\"0\" marginwidth=\"0\" onload=\"javascript:if(document.frames('ifr_app_note_list').document.getElementById('tbl_note_list')==null){document.getElementById('ifr_app_note_list').height=0;}else{document.getElementById('ifr_app_note_list').height=document.frames('ifr_app_note_list').document.getElementById('tbl_note_list').clientHeight;}\" src=\"/PUBPAGE/flow_note_list.jsp?note_kind=L&db_alias=<%=view.getDBAlias()%>&db_debug=<%=view.getDBDebug()%>&source_charset=<%=view.getSourceCharset()%>&target_charset=<%=view.getTargetCharset()%>&flow_num=<%=view.getflow_num()%>\">",
                            1);
        super.outputPageTag(pbwrPrgNO, gstbTagTabStr, gstrTabChar, "</iframe>",
                            1);
    }


}
